package com.fulihui.wgj.schedule.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.fulihui.common.enums.SendLogStatusEnum;
import com.fulihui.common.result.rpc.BaseResult;
import com.fulihui.common.result.rpc.SimpleResult;
import com.fulihui.common.util.RpcResultCheckUtil;
import com.fulihui.wgj.facade.dto.Wx48NewsLogDTO;
import com.fulihui.wgj.facade.request.wx48.Wx48NewsCrowdRequest;
import com.fulihui.wgj.facade.request.wx48.Wx48NewsLogQueryRequest;
import com.fulihui.wgj.facade.request.wx48.Wx48NewsSendRequest;
import com.fulihui.wgj.facade.service.wx48.Wx48NewsInfoService;
import com.fulihui.wgj.schedule.annotation.MutexLock;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 微信48小时消息定时发送
 *
 * @author wang_wx
 * @date 2018-07-20
 */
@Component
public class Wx48HSendJob {

    private static final Logger LOGGER = LoggerFactory.getLogger(Wx48HSendJob.class);

    @Reference(version = "1.0.0")
    Wx48NewsInfoService wx48NewsInfoService;

    @MutexLock(node = "${job.node.wx48HSendJob_execute}")
    @Scheduled(cron = "${job.cron.wx48HSendJob_execute}")
    public void execute() {
        LOGGER.info("Wx48HSendJob start");
        Wx48NewsLogQueryRequest wx48NewsLogQueryRequest = new Wx48NewsLogQueryRequest();
        // 查询定时(等待发送的任务)
        wx48NewsLogQueryRequest.setStatus(SendLogStatusEnum.await.getCode());
        SimpleResult<List<Wx48NewsLogDTO>> newsLog = wx48NewsInfoService.queryNewsLog(wx48NewsLogQueryRequest);
        LOGGER.info("newsLogResult = {}", newsLog);
        if (RpcResultCheckUtil.isSuccess(newsLog)) {
            List<Wx48NewsLogDTO> newsLogData = newsLog.getData();
            if (!CollectionUtils.isEmpty(newsLogData)) {
                newsLogData.forEach(wx48NewsLogDTO -> {
                    Date jobTime = wx48NewsLogDTO.getJobTime();
                    // 定时任务时间到了 进行发送
                    if (Objects.nonNull(jobTime) && jobTime.getTime() <= System.currentTimeMillis()) {
                        LOGGER.info("job wx48NewsLogDTO = {}", wx48NewsLogDTO);
                        Long id = wx48NewsLogDTO.getId();
                        String appId = wx48NewsLogDTO.getAppId();
                        Long newsId = wx48NewsLogDTO.getNewsId();
                        String sendCondition = wx48NewsLogDTO.getSendCondition();
                        Wx48NewsSendRequest wx48NewsSendRequest = new Wx48NewsSendRequest();
                        wx48NewsSendRequest.setAppId(appId);
                        wx48NewsSendRequest.setNewId(newsId);
                        wx48NewsSendRequest.setRecordId(id);
                        if (StringUtils.isNotBlank(sendCondition)) {
                            wx48NewsSendRequest.setWx48NewsCrowdRequest(JSON.parseObject(sendCondition, Wx48NewsCrowdRequest.class));
                        }
                        BaseResult baseResult = wx48NewsInfoService.sendWx48NewsTask(wx48NewsSendRequest);
                        if (baseResult.isSuccess()) {
                            LOGGER.info("48H job recordId = {}执行成功！", id);
                        } else {
                            LOGGER.info("48H job recordId = {}执行失败！", id);
                        }
                    }
                });
            }
        }
        LOGGER.info("Wx48HSendJob end");
    }

}
